package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R saveAddress(@RequestBody AddressBook addressBook) {
        //获取当前登录用户
        //  Long userId = (Long) session.getAttribute("user");
        Long currentId = BaseContext.getCurrentId();
        //设置userId的值
        addressBook.setUserId(currentId);
        boolean flag = addressBookService.save(addressBook);
        return flag ? R.success("保存地址成功") : R.error("保存地址失败");

    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();

        //先将该用户所有地址的is_default设置为0
        qw.set(AddressBook::getIsDefault,0);
        qw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
         addressBookService.update(qw);

        //将当前的设置的默认地址的is_default设置为1
        LambdaUpdateWrapper<AddressBook> qwd = new LambdaUpdateWrapper<>();
        qwd.eq(addressBook.getId() != null,AddressBook::getId,addressBook.getId() );
        qwd.set(AddressBook::getIsDefault,1);
        boolean flag = addressBookService.update(qwd);
        return flag?R.success("设置成功"):R.error("设置失败");
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R queryAddress(@PathVariable Long id){
       // LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        //qw.eq(id !=null,AddressBook::getId,id);
        AddressBook addressBook = addressBookService.getById(id);
        return  R.success(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> queryDefaultAddress(){
        //获取当前登录用户
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(currentId!= null,AddressBook::getUserId,currentId);
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);
        return R.success(one);
    }

    /**
     * 查询指定用户的全部地址
     * @return
     */
    @GetMapping("/list")
    public R queryByUser(){
        //获取当前登录用户
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(currentId!= null,AddressBook::getUserId,currentId);
        List<AddressBook> list = addressBookService.list(qw);
        return  R.success(list);
    }

    /**
     * 删除地址(可以是多个)
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteAddressBook(@RequestParam List<Long> ids){
        boolean flag = addressBookService.removeByIds(ids);
        return flag?R.success("删除成功"):R.error("删除失败");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R updateAddressBook(@RequestBody AddressBook addressBook){
        /*LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(addressBook!=null,AddressBook::getId,addressBook.getId());*/
        boolean flag = addressBookService.updateById(addressBook);
        return  flag?R.success("修改成功"):R.error("修改失败");

    }
}
